/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package cs4280.dao;



import cs4280.bean.Booking;
import cs4280.jdbc.DB;
import java.util.ArrayList;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.NamingException;
import javax.sql.DataSource;
import cs4280.bean.*;
import java.util.Iterator;

/**
 *
 * @author PangPei
 */
public class CounterDAO {
   public Counter getCounter() {
        Counter counter=new Counter();

        try {
            DataSource ds = DB.getDataSource();
            Connection conn = ds.getConnection();

            PreparedStatement pstmt=conn.prepareStatement("SELECT [count] FROM [counter] ;");
            ResultSet rs=pstmt.executeQuery();
            while (rs != null && rs.next() != false) {
                counter.setCount(rs.getInt(1));
            }
            if (rs != null) {
                rs.close();
            }
            if (pstmt != null) {
                pstmt.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (javax.naming.NamingException ne) {
            /* ignore */
        } catch (SQLException sqle) {
            /* ignore */
        }

        return counter;
    }

    public void changeCount(int count) {

        try {
            DataSource ds = DB.getDataSource();
            Connection conn = ds.getConnection();
            PreparedStatement pstmt = conn.prepareStatement("UPDATE [counter] SET count=? WHERE id= 1");
            pstmt.setInt(1, count);

            int rows = pstmt.executeUpdate();


            if (pstmt != null) {
                pstmt.close();
            }
            if (conn != null) {
                conn.close();
            }


        } catch (NamingException ne) {

            /*ignore*/
        } catch (SQLException sqle) {

            /*ignore*/
        }

    }

}
